package org.eclipse.football;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
import java.util.ArrayList;

public class LogIn {

	//first string is username, second is password
	private String[] user = new String[2];
	
	public LogIn(String username, String password){
		this.user[0]=username;
		this.user[1]=password;
	}

	//see if username and password combo is known
	public boolean isKnown(){
		// Instantiate file object with input file name
		File in_file = new File("Users.txt");
		
		//ArrayList to store users in
		ArrayList<String[]> users = new ArrayList<String[]>();
		users = read_Users_file(in_file);	
		
		//see if the user is known.
		//I didn't feel like writing an efficient search algorithm.
		//If we get a bunch of users a more efficient algorithm is needed.
		for(int i=0;i<users.size();i++){
			if(users.get(i)[0].equals(this.user[0])&&users.get(i)[1].equals(this.user[1])){
				return true;
			}
		}
		return users.contains(this.user);
	}
	
	// see if user exists
	public boolean exists(){
		// Instantiate file object with input file name
		File in_file = new File("Users.txt");
				
		//ArrayList to store users in
		ArrayList<String[]> users = new ArrayList<String[]>();
		users = read_Users_file(in_file);	
		
		//scan through list to see if name is known
		//I didn't feel like writing an efficient search algorithm
		//If we get a bunch of users a more efficient algorithm is needed.
		for(int i=0;i<users.size();i++){
			if(users.get(i)[0].equals(this.user[0])){
				return true;
			}
		}
		
		return false;
	}
	
	public void delete(){
		// Instantiate file object with input file name
		File file = new File("Users.txt");
						
		//ArrayList to store users in
		ArrayList<String[]> users = new ArrayList<String[]>();
		users = read_Users_file(file);
		int index=0;
		
		for(int i=0;i<users.size();i++){
			if(users.get(i)[0].equals(this.user[0])){
				index = i;
			}
		}
		
		users.remove(index);
		
		write_Users_file(file, users);
		
	}
	
	//add user name to file
	public void add(){
		//ArrayList to store users in
		ArrayList<String[]> users = new ArrayList<String[]>();
				
		// Instantiate file object with  Users.txt file name
		File file = new File("Users.txt");

		//read file
		users = read_Users_file(file);
		
		//add new user
		users.add(this.user);

		//write new Users.txt file
		write_Users_file(file,users);
	}
	
	public ArrayList<String[]> read_Users_file(File in_file){
		//ArrayList to store users in
		ArrayList<String[]> users = new ArrayList<String[]>();
				
		//Read in current users
				
		int n_users = 0;					
		// Check if in_file exists
		if(in_file.exists()&&in_file.length()!=0){
			Scanner scanner;
			try {
				scanner = new Scanner(new FileReader(in_file));
								
				// Read the number of users from the first line of the file
				n_users = Integer.parseInt(scanner.nextLine().split("\n")[0]);
						
				for(int i = 0;i<n_users;i++){
					users.add(scanner.nextLine().split("\t"));
				}
			}
			catch (FileNotFoundException e) {
				//do nothing
			}
		}
		else {
			//System.out.println("Users.txt not found");
		}	
		return users;
	}
	
	public void write_Users_file(File out_file, ArrayList<String[]> users){
		int n_users=users.size();
		// Instantiate a new buffered file writer
		Writer output;
		try {
			output = new BufferedWriter(new FileWriter(out_file));
			
			// Write number of users
			output.write(String.format("%d\n", n_users));
			
			//write users, line by line
			for(int i=0;i<n_users;i++){
			output.write(users.get(i)[0]+"\t"+users.get(i)[1]+ "\n");
			}
			
			output.close();
		} catch (IOException e) {
			// Do nothing
		}	
	}
}